package com.dy.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Auther: dy
 * @Date: 2022/4/16 - 04 - 16 - 16:52
 * @Description: com.dy.config
 * @version: 1.0
 */
@Configuration
public class RabbitMqConfig {
    @Autowired
    private CachingConnectionFactory cachingConnectionFactory;

    @Bean//配置rabbitmq模板
    public RabbitTemplate rabbitTemplate(){
        return new RabbitTemplate(cachingConnectionFactory);
    }

    @Bean//配置消息队列
    public Queue queue(){
        return new Queue("orderVoQueue");
    }

    @Bean//配置交换机
    public DirectExchange directExchange(){
        return new DirectExchange("directExchange");
    }

    @Bean//绑定消息队列到交换机并命名
    public Binding binding(@Autowired Queue queue,@Autowired DirectExchange directExchange){
        return BindingBuilder.bind(queue).to(directExchange).with("order.queue");
    }
}
